<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;
use Admin\Common\BaseController;
/**
 * Description of ArticleController
 *
 * @author zhaokai
 */
class ArticleController extends BaseController {
    //put your code here
    public function index(){
        
        $Category = D("Category");
        $data = $Category->select();
        $this->assign("categories", $data);
        $this->display();
    }
    
    public function getArticleList(){
        $Article = D("Article");
        $categoryId = I("post.categoryId");
        $title = I("post.title");
        
        $condition = array();
        if($categoryId != NULL){
            $condition["categoryId"] = $categoryId;
        }
        if($title != NULL){
            $condition["title"] = "%".$title."%";
        }
        $data["data"] = $Article->where($condition)->select();
        //dump($data);
        $this->ajaxReturn($data, "JSON");
    }
    
    public function getArticle(){
        
        
    }
    
    public function edit(){
        $Category = D("Category");
        $data = $Category->select();
        $this->assign("categories", $data);
        $id = I("get.id");
        if($id != NULL){
            $Article = D("Article");
            $article = $Article->where("id=".$id)->find();
            $this->assign("article", $article);
            
            $Seo = D("Seo");
            $seo = $Seo->where("id=".$id)->find();
            $this->assign("seo", $seo);
        }
        $this->display();
    }
    
    public function save(){
        $Article = D("Article");
        $data['categoryId'] = I("post.categoryId");
        $data['status'] =  I("post.status");
        $data['ballStar'] =  I("post.ballStar");
        $data['snapshot'] =  I("post.snapshot");
        $data['videoUrl'] =  I("post.videoUrl");
        $data['title'] =  I("post.title");
        $data['author'] =  I("post.author");
        $data['source'] =  I("post.source");
        $data['label'] =  I("post.label");
        $data['abstract'] =  I("post.abstract");
        $data['content'] =  I("post.content");
        $data['alias'] =  I("post.alias");
        
        $id = I("post.id");
        
        $result = false;
        if($id == ""){
            $data['recordTime'] = date('Y-m-d H:i:s');
            $id = $Article->add($data);
            $result = true;
        }else{
            $Article->where('id = '.$id)->save($data);
            $result = true;
        }
        if($result){
            $Seo = D("Seo");
            $count = $Seo->where("id=".$id)->count();
            $seo["title"] = I("post.seo_title");
            $seo["keywords"] = I("post.seo_keywords");
            $seo["description"] = I("post.seo_description");
            if($count > 0){
                $Seo->where('id = '.$id)->save($seo);
            }else{
                $seo["id"] = $id;
                $Seo->add($seo);
            }
        }
        echo $result;
    }
    
    public function delete(){
        $id = I("post.id");
        $Article = D("Article");
        $result = $Article->where("id = ".$id)->delete();
        $this->ajaxReturn($result);
    }
    
    
}
